package login;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")
public class loginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if(!"lisi".equals(username) || !"123".equals(password)){
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("错误");
            return;
        }
        HttpSession session = req.getSession(true);//true:如果不存在，就自动创建一个session
        session.setAttribute("username", username);
        session.setAttribute("loginTime", System.currentTimeMillis());
        //解决cookie禁用后，session的跟踪问题
        //String uri = resp.encodeRedirectURL("index");
        //System.out.println(session.getAttribute("username")+" "+session.getAttribute("loginTime"));
        //resp.sendRedirect(uri);
        resp.sendRedirect("index");
    }
}
